package login;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;
import io.ClientSession;
import io.OperationResponse;

/**
 * 
 * @author Zhengjiangyu
 *
 */
public class HMLogin {
	protected static final Logger log = LoggerFactory.getLogger(HMLogin.class);

	public static void login(OperationResponse operationRequest,
			ClientSession session) {
		try {

			if (operationRequest.Parameters.size() < 2) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, null, ReturnCode.No, "");
				session.SendOperationResponse(response);
			}
			String id = (String) operationRequest.Parameters
					.get(ParameterType.UserID);
			String token = (String) operationRequest.Parameters
					.get(ParameterType.UserPwd);
			String deviceID = (String) operationRequest.Parameters
					.get(ParameterType.DeviceID);
			String isGetIP = (String) operationRequest.Parameters
					.get(ParameterType.IsGetIP);
			String PhoneType=(String) operationRequest.Parameters
					.get(ParameterType.PhoneType);

			if (id.equals("zealm") && token.equals("")) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, AppConfig.dicAppInfo,
						ReturnCode.GetServer, "");
				session.SendOperationResponse(response);
				return;

			}
			id = "HM" + id;
			operationRequest.Parameters.put(ParameterType.UserID, id);
			HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
			myPrams.put(ParameterType.UserID, id);
			OperationResponse responseID = new OperationResponse(
					operationRequest.OperationCode, myPrams, ReturnCode.HasID,
					"");
			session.SendOperationResponse(responseID);
			Util.sdkReceived(operationRequest, session, id, deviceID, isGetIP,"HM",PhoneType,"");

		
		} catch (Exception e) {
			log.error("HM return hava a error result="+e.toString());
			// TODO: handle exception
		}
		
	}
	public  Runnable getLoginHM(OperationResponse operationResponse, ClientSession session){ 
		class LoginHM implements Runnable { 
			private OperationResponse t_operationResponse;
			private ClientSession t_session;
			public LoginHM(OperationResponse operationResponse, ClientSession session)
			{
				t_operationResponse = operationResponse;
				t_session = session;
			}
			
			@Override
			public void run() {
					try {
						login(t_operationResponse, t_session);
					} catch (Exception e) {
						log.info(Thread.currentThread().getName() + " HMLogin TASK Error!!!");
					}
				}
        }
	    return new LoginHM(operationResponse, session); 
	 } 
}
